package cn.wolfcode.shop.msg;

import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.vo.OrderVo;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;

@Component
public class OrderConsumer {


    @Autowired
    private IOrderInfoService orderService;

    //监听队列模式
    @JmsListener(destination = "shop.order", containerFactory = "jmsListenerContainerQueue")

    //接收消息
    public void receiveMsgQueue(Message message) throws JMSException {

        if (message instanceof MapMessage) {
            MapMessage mapMessage = (MapMessage) message;
            //从消息中获取
            Long userId = mapMessage.getLong("userId");
            String orderVoStr =mapMessage.getString("orderVo");
            OrderVo orderVo = JSON.parseObject(orderVoStr,OrderVo.class);
            //调用生成订单方法
            orderService.generateOrder(userId,orderVo);
        }

    }

    //监听发布订阅模式
   /* @JmsListener(destination = "shop.topic", containerFactory = "jmsListenerContainerTopic")

    //接收消息
    public void receiveMsgTopic(Message message) throws JMSException {
        System.out.println("topic" + message);

        if (message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
            System.out.println(textMessage.getText());
        }

    }*/
}
